﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using Telerik.Windows.Controls;
using Broker.Model;
using BrokerBase.Lib.Tool;

namespace Broker
{
    /// <summary>
    /// Interaction logic for UserPasswordForm.xaml
    /// </summary>
    public partial class UserPasswordForm
    {
        private SysUser user;

        public UserPasswordForm(SysUser user)
        {
            this.user = user;
            InitializeComponent();
            Loaded += new RoutedEventHandler(UserPasswordForm_Loaded);
        }

        void UserPasswordForm_Loaded(object sender, RoutedEventArgs e)
        {
            
            DataContext = user;
        }

        private void btnConfirm_Click(object sender, RoutedEventArgs e)
        {
            string pass1 = txtOriPassword.Password;

            if (txtNewPassword1.Password.Trim() != txtNewPassword2.Password.Trim())
            {
                MessageBox.Show("重复密码不正确!");
                return;
            }

            if (!user.CheckLoginPassword(AppGlobal.HbmSessionFactory.GetCurrentSession(), pass1))
            {
                MessageBox.Show("原口令不正确!");
                return;
            }

            try
            {
                user.Password = txtNewPassword1.Password;

                DBServiceUtil.GetService<SysUser>(AppGlobal.HbmSessionFactory.GetCurrentSession()).SaveOrUpdate(user);

                MessageBox.Show("口令修改成功","提示");
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}
